package com.net.oil.user.model;

import com.net.oil.bean.BaseBean;
import com.net.oil.base.BaseModel;
import com.net.oil.http.AppHttpClient;
import com.net.oil.http.custom.BaseObserver;
import com.net.oil.http.custom.BaseObserverNoT;
import com.net.oil.user.bean.UserInfo;
import com.net.oil.user.contract.LoginContract;
import com.net.oil.user.presenter.LoginPresenter;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：lch
 * 时间：2018/3/14:9:48
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class LoginModel extends BaseModel implements LoginContract.Model {

    private LoginPresenter presenter;

    public LoginModel(LoginPresenter presenter) {
        this.presenter = presenter;
        compositeDisposable = new CompositeDisposable();
    }

    @Override
    public void login(String phone, final String password) {
        AppHttpClient.ApiService().login(phone, password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<UserInfo>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull BaseBean<UserInfo> tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null && tResultBean.getData() != null){
                            presenter.loginSuccess(tResultBean.getData());
                        }else {
                            presenter.loginFailed();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                        presenter.loginFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    /**
     * 获取手机验证码(没有返回值)
     *
     * @param phone 手机号
     * @param ptt   是否是语音
     */
    @Override
    public void getPhoneCode(String phone, boolean ptt) {
        AppHttpClient.ApiService().getPhoneCode(phone, ptt)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserverNoT() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull BaseBean tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null) {
                            presenter.getPhoneCodeSuccess();
                        } else {
                            presenter.getPhoneCodeFailed();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                        presenter.getPhoneCodeFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    @Override
    public void loginByAuthCode(final String phone, final String code) {
        AppHttpClient.ApiService().loginByAuthCode(phone, code)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<UserInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean<UserInfo> tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null && tResultBean.getData() != null){
                            presenter.loginByAuthCodeSuccess(tResultBean.getData());
                        }else {
                            presenter.loginByAuthCodeFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.loginByAuthCodeFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }
}
